﻿#include "editfinancesubject.h"
#include "ui_editfinancesubject.h"

EditFinanceSubject::EditFinanceSubject(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::EditFinanceSubject)
{
    ui->setupUi(this);

    setWindowTitle("编辑科目");
}

EditFinanceSubject::~EditFinanceSubject()
{
    delete ui;
}

void EditFinanceSubject::setDirectory(QList<QStringList> directory){
    if(directory.size()!=3){
        return;
    }
    this->directory.clear();
    this->directory.append(directory[1]);
    this->directory.append(directory[2]);
    ui->comboBoxPrimaryDirectory->setCurrentIndex(0);
    ui->comboBoxSecondaryDirecctory->clear();
    ui->comboBoxSecondaryDirecctory->addItems(this->directory[0]);
}

void EditFinanceSubject::showWithFinanceItem(FinanceItem *financeItem){
    this->financeItem = financeItem;
    ui->comboBoxPrimaryDirectory->setCurrentIndex(financeItem->type);
    ui->comboBoxSecondaryDirecctory->setCurrentText(financeItem->category);
    ui->lineEditSubjectOld->setText(financeItem->subject);
    ui->lineEditSubjectNew->clear();

    exec();
}

void EditFinanceSubject::on_comboBoxPrimaryDirectory_currentIndexChanged(int index)
{
    ui->comboBoxSecondaryDirecctory->clear();
    ui->comboBoxSecondaryDirecctory->addItems(directory[index]);
}

void EditFinanceSubject::on_pushButtonCancel_clicked()
{
    this->close();
}

void EditFinanceSubject::on_pushButtonDelete_clicked()
{
    financeService.deleteFinanceItem(this, financeItem);
}

void EditFinanceSubject::on_pushButtonUpdate_clicked()
{
    financeItem->type = ui->comboBoxPrimaryDirectory->currentIndex();
    financeItem->category = ui->comboBoxSecondaryDirecctory->currentText();
    financeItem->subject = ui->lineEditSubjectNew->text();

    financeService.updateFinanceItem(this, financeItem);
}
